<?php


namespace app\controller\admin\verify;


use app\common\exception\ApiException;
use app\controller\admin\BaseAdminController;
use app\model\cart\VerifyCartModel;
use app\model\user\UserModel;
use app\service\utils\QqMapService;

class VerifyCartOrderController extends BaseAdminController
{

    function setModel()
    {
        return new VerifyCartModel();
    }

    function setWithModel()
    {
        return ['coach'];
    }

    function setSearch()
    {
        return [
            'like' => ['order_sn', 'name', 'mobile', 'coach.username' => 'coach_name', 'source'],
            '=' => ['status']
        ];
    }

    function setSort()
    {
        // TODO: Implement setSort() method.
    }


    public function save()
    {
        try {
            $id = $this->request->param('id', 0);

            $data = $this->request->only(['mobile', 'name', 'coach_user_id', 'shape_id', 'reserve_time', 'amount',
                'status', 'address', 'remark', 'source', 'condition', 'cart_type', 'is_trial', 'brand']);
            $data['reserve_time'] = strtotime($data['reserve_time']);

            $userId = UserModel::where('mobile', $data['mobile'])->value('id');
            if (!$userId) {
                throw new ApiException('用户不存在');
            }
            $data['user_id'] = $userId;

            $qqMapService = new QqMapService();
            $result = $qqMapService->getCoordinate($data['address']);
            $data['lng'] = $result['lng'];
            $data['lat'] = $result['lat'];
            if (!$id) {
                $orderInfo = $this->setModel()->where('mobile', $data['mobile'])
                    ->where('source', '<>', '平台')
                    ->where('order_type', 1)
                    ->find();
                if ($orderInfo) {
                    throw new ApiException('一个手机号只能添加一次');
                }
                $data['order_sn'] = createOrderSn('partner_order', 'order_sn', 'YC');

                $partnerOrderModel = clone $this->setModel();
                $partnerOrderModel->order_sn = $data['order_sn'];
                $partnerOrderModel->user_id = $data['user_id'];
                $partnerOrderModel->coach_user_id = $data['coach_user_id'] ?? 0;
                $partnerOrderModel->shape_id = $data['shape_id'];
                $partnerOrderModel->condition = $data['condition'];
                $partnerOrderModel->cart_type = $data['cart_type'];
                $partnerOrderModel->amount = $data['amount'];
                $partnerOrderModel->name = $data['name'];
                $partnerOrderModel->mobile = $data['mobile'];
                $partnerOrderModel->lng = $data['lng'];
                $partnerOrderModel->lat = $data['lat'];
                $partnerOrderModel->address = $data['address'];
                $partnerOrderModel->status = 2;
                $partnerOrderModel->reserve_time = $data['reserve_time'];
                $partnerOrderModel->reserve_type = 2;
                $partnerOrderModel->order_type = 1;
                $partnerOrderModel->brand = $data['brand'];

                $partnerOrderModel->remark = $data['remark'] ?? "";
                $partnerOrderModel->is_trial = $data['is_trial'] ?? 0;
                $partnerOrderModel->source = $data['source'] ?? '';
                $partnerOrderModel->save();
            } else {
                $partnerOrderModel = $this->setModel()->find($id);
                $partnerOrderModel->save($data);
            }
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    public function getStatusArr()
    {
        return $this->setModel()->getStatusArr();
    }
}